package club.gskj.gulimall.search.controller;

import club.gskj.common.exception.BizCodeEnume;
import club.gskj.common.to.SkuHasStockVo;
import club.gskj.common.to.es.SkuEsModel;
import club.gskj.common.utils.R;
import club.gskj.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName EslasticSaveController
 * @Description TODO
 * @Author 康纳
 * @Date 2021-02-10
 */


@Slf4j
@RequestMapping("/search/save")
@RestController
public class EslasticSaveController {


    @Autowired
    private ProductSaveService productSaveService;

    /***
     *
     * @param skuEsModels
     * @return club.gskj.common.utils.R
     * @author 康纳-connor
     * @creed: Talk is cheap,show me the code
     * @date 21.2.10 19:34
     * @Description 上架商品
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean b = false;
        try {
            b = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("EslasticSaveController 商品上架异常: {}", e);
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (!b) {
            return R.ok();
        }
        return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
    }
}
